﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CoinEffectMgr : MonoBehaviour
{
    [Header("金币预制体")]
    public GameObject Coin;
    [Header("金币数量")]
    public int num;
    [Header("最终目的地")]
    public Transform targetPos;
    [Header("金币飞行速度")]
    public float time = 1;
    public void StartEffect()
    {
        if (transform.GetComponent<RectTransform>() != null)
        {
            for (int i = 0; i < num; i++)
            {
                GameObject go = Instantiate(Coin, Vector3.zero, Quaternion.identity) as GameObject;
                //UICurrencyCollect go = GameObject.Instantiate(Coin, transform.localPosition, Quaternion.identity, transform);
                go.transform.SetParent(transform);
                go.transform.localPosition = new Vector3(0, 0, 0);
                float scale = Random.Range(0.8f, 1.1f);
                go.transform.localScale = new Vector3(scale, scale, 1);
                //go.InitAnimation();
                UICurrencyCollect cc = go.GetComponent<UICurrencyCollect>();
                if (cc != null)
                {
                    cc.SetFlyParam(time);
                    cc.InitAnimation(targetPos);
                }
            }
        }
        else
        {
            GameObject go = GameObject.Find("Canvas");
            if (go == null)
            {
                go = new GameObject("Canvas").AddComponent<Canvas>().gameObject;
            }

            GameObject CoinParent = GameObject.Find("CoinParent");
            if (CoinParent == null)
            {
                CoinParent = new GameObject("CoinParent");
                CoinParent.AddComponent<RectTransform>();
                CoinParent.transform.SetAsLastSibling();
            }
            else
            {
                for (int i = 0; i < CoinParent.transform.childCount; i++)
                {
                    Destroy(CoinParent.transform.GetChild(i).gameObject);
                }
            }
            RectTransform rect = go.GetComponent<RectTransform>();
            CoinParent.transform.SetParent(go.transform);
            Vector3 ObjWorldPos = transform.position;
            Vector3 uiObjectPos = WorldToUGUIPosition(rect, Camera.main, ObjWorldPos);
            //CoinParent.transform.localPosition = uiObjectPos;
            CoinParent.transform.GetComponent<RectTransform>().anchoredPosition = uiObjectPos;
            for (int i = 0; i < num; i++)
            {
                GameObject coinGo = Instantiate(Coin, Vector3.zero, Quaternion.identity) as GameObject;
                coinGo.transform.SetParent(CoinParent.transform);
                coinGo.transform.localPosition = new Vector3(0, 0, 0);
                //go.InitAnimation();
                UICurrencyCollect cc = coinGo.GetComponent<UICurrencyCollect>();
                if (cc != null)
                {
                    cc.SetFlyParam(time);
                    cc.InitAnimation(targetPos);
                }
            }
        }
    }

    public static Vector2 WorldToUGUIPosition(RectTransform canvasRectTransform, Camera camera, Vector3 worldPosition)
    {
        //世界坐标-》ViewPort坐标   
        Vector2 viewPos = camera.WorldToViewportPoint(worldPosition);

        //ViewPort坐标-〉UGUI坐标
        //return new Vector2(canvasRectTransform.rect.width * viewPos.x, canvasRectTransform.rect.height * viewPos.y);
        //因为默认的CoinParent锚点是中间位置。使用上方的代码需要设置为左下角锚点
        return new Vector2(canvasRectTransform.rect.width * viewPos.x - canvasRectTransform.rect.width * 0.5f, canvasRectTransform.rect.height * viewPos.y - canvasRectTransform.rect.height * 0.5f);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartEffect();
        }
    }
}
